package com.yangxk.kadmin.web.module.sample.controller;

import com.yangxk.kadmin.web.module.sample.form.CategoryEditForm;
import com.yangxk.kadmin.web.module.sample.form.CategoryQueryForm;
import com.yangxk.kadmin.web.module.sample.service.CategoryService;
import com.yangxk.kadmin.web.module.sample.po.Category;
import com.yangxk.kadmin.common.beans.Result;
import com.yangxk.kadmin.web.common.annotation.Permission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/category/all")
    public Result queryAll() {
        return Result.ofSuccess(categoryService.queryAll());
    }

    @GetMapping("/category/list")
    public Result<PageInfo> queryByPage(CategoryQueryForm categoryQueryForm) {
        return Result.ofSuccess(categoryService.queryByPage(categoryQueryForm));
    }

    @PostMapping("/category/add")
    @Permission("category:add")
    public Result add(@RequestBody CategoryEditForm form) {
        categoryService.addCategory(form);
        return Result.ofSuccess();
    }

    @GetMapping("/category/detail")
    public Result<Category> queryById(long id) {
        Category category = categoryService.selectByPrimaryKey(id);
        return Result.ofSuccess(category);
    }

    @PostMapping("/category/update")
    @Permission("category:update")
    public Result update(@RequestBody CategoryEditForm form) {
        categoryService.updateCategory(form);
        return Result.ofSuccess();
    }

    @GetMapping("/category/delete")
    @Permission("category:delete")
    public Result delete(long id) {
        categoryService.deleteCategory(id);
        return Result.ofSuccess();
    }

}
